<template>
    <div class="addAddress">
        <div class="topBox">
            <img class="leftImg" src="../../static/imgs/left_b.png" @click="handleBack" alt="">
            <div class="title">
                {{ titleType == 0 ? $t('assemble.新增地址') : $t('assemble.编辑地址') }}
            </div>
        </div>

        <div class="inputBox">
            <input class="inputItem" type="text" v-model="company" :placeholder="$t('assemble.请输入公司名称')">
            <input class="inputItem" type="text" v-model="name" :placeholder="$t('assemble.姓名')">
            <input class="inputItem" type="text" v-model="mobile" :placeholder="$t('assemble.手机号')">
            <input class="inputItem" type="text" v-model="site" :placeholder="$t('assemble.请输入收件地址')">
            <input class="inputItem" type="text" v-model="postcode" :placeholder="$t('assemble.邮编')">
        </div>



        <div class="defultBox" @click="defaults = !defaults">
            <img v-if="!defaults" class="statusimg" src="../../static/imgs/null.png" alt="">
            <img v-else class="statusimg" src="../../static/imgs/fill.png" alt="">
            <div>默认</div>
        </div>


        <div class="btn" @click="handleSave">
            保存
        </div>

    </div>
</template>
<script>
import { Toast, Notify } from 'vant';
import { addAdress, editAdress, delAdress } from '../..//utils/api'

export default {
    components: {

    },
    data() {
        return {
            content: "",
            lang: 'zh',
            titleType: null,
            company: '',//公司名称
            name: '',//姓名
            mobile: '',//手机号
            site: '',//收件地址
            postcode: '',//邮编
            defaults: false,
            addressId: "",

        }
    },
    created() {
        this.$i18n.locale = localStorage.getItem('langtype')
        this.lang = localStorage.getItem('langtype')
        this.titleType = this.$route.query.type
        if (this.$route.query.type == 1) {
            this.company = JSON.parse(localStorage.getItem('addressData')).company
            this.name = JSON.parse(localStorage.getItem('addressData')).name
            this.mobile = JSON.parse(localStorage.getItem('addressData')).mobile
            this.site = JSON.parse(localStorage.getItem('addressData')).site
            this.postcode = JSON.parse(localStorage.getItem('addressData')).postcode
            this.defaults = JSON.parse(localStorage.getItem('addressData')).default
            this.addressId = JSON.parse(localStorage.getItem('addressData')).id
        }

    },
    mounted() {
        // this.getyinsi()
    },
    methods: {
        async handleSave() {
            if (this.company == "") {
                Toast(this.$t('assemble.请输入公司名称'));
                return
            }
            if (this.name == "") {
                Toast(this.$t('assemble.请输入姓名'));
                return
            }
            if (this.mobile == "") {
                Toast(this.$t('assemble.请输入手机号'));
                return
            }
            if (this.site == "") {
                Toast(this.$t('assemble.请输入收件地址'));
                return
            }
            if (this.postcode == "") {
                Toast(this.$t('assemble.请输入邮编'));
                return
            }
            if (this.titleType == 0) {
                let data = {
                    company: this.company,
                    name: this.name,
                    mobile: this.mobile,
                    site: this.site,
                    postcode: this.postcode,
                    default: this.defaults ? 1 : 0,
                }
                let res = await addAdress(data)
                if (res.code == 1) {
                    Toast(this.$t('assemble.添加成功'));
                    setTimeout(() => {
                        this.handleBack()
                    }, 1500)
                } else {
                    Toast(res.msg);
                }

            } else {
                let data = {
                    id: this.addressId,
                    company: this.company,
                    name: this.name,
                    mobile: this.mobile,
                    site: this.site,
                    postcode: this.postcode,
                    default: this.defaults ? 1 : 0,
                }
                let res = await editAdress(data)
                if (res.code == 1) {
                    Toast(this.$t('assemble.编辑成功'));
                    setTimeout(() => {
                        this.handleBack()
                    }, 1500)
                } else {
                    Toast(res.msg);
                }
            }

        },
        handleBack() {
            this.$router.back(1)
        },
    },

}
</script>

<style scoped lang="scss">
@media screen and (max-width: 750px) {
    .topBox {
        width: 100%;
        height: 44px;
        // padding: 10px 15px;
        padding-top: 10px;
        position: relative;
        font-family: PingFang SC, PingFang SC;
        font-weight: 600;
        font-size: 16px;
        color: #333333;
        line-height: 19px;
        text-align: left;
        font-style: normal;
        text-transform: none;

        .leftImg {
            width: 24px;
            height: 24px;
            position: absolute;
            top: 10px;
            left: 15px;
        }

        .title {
            width: 100%;
            text-align: center;

        }
    }

    .inputBox {
        .inputItem {
            display: block;
            width: 90%;
            height: 42px;
            border-radius: 4px 4px 4px 4px;
            border: 1px solid #EDEDED;
            margin: 20px auto;
            font-family: PingFang SC, PingFang SC;
            font-weight: 400;
            font-size: 16px;
            line-height: 19px;
            text-align: left;
            font-style: normal;
            text-transform: none;
            text-indent: 10px;
        }
    }

    .defultBox {
        width: 100%;
        padding: 0 15px;
        box-sizing: border-box;
        display: flex;
        align-items: center;
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        font-size: 16px;
        color: #B1B1B1;
        line-height: 19px;
        text-align: left;
        font-style: normal;
        text-transform: none;

        .statusimg {
            width: 16px;
            height: 16px;
            margin-right: 10px;
        }
    }

    .btn {
        width: 320px;
        height: 44px;
        background: #E00000;
        border-radius: 100px 100px 100px 100px;
        font-family: PingFang SC, PingFang SC;
        font-weight: 500;
        font-size: 15px;
        color: #FFFFFF;
        line-height: 44px;
        text-align: center;
        font-style: normal;
        text-transform: none;
        margin: 0 auto;
        position: absolute;
        bottom: 50px;
        left: 50%;
        margin-left: -160px
    }


}

@media screen and (min-width: 751px) and (max-width: 1300px) {
    .topBox {
        width: 100%;
        height: 88px;
        // padding: 10px 15px;
        padding-top: 20px;
        position: relative;
        font-family: PingFang SC, PingFang SC;
        font-weight: 600;
        font-size: 25px;
        color: #333333;
        line-height: 19px;
        text-align: left;
        font-style: normal;
        text-transform: none;
        display: flex;
        align-items: center;

        .leftImg {
            width: 48px;
            height: 48px;
            position: absolute;
            top: 40px;
            left: 30px;
        }

        .title {
            width: 100%;
            text-align: center;

        }
    }

    .inputBox {
        .inputItem {
            display: block;
            width: 90%;
            height: 84px;
            border-radius: 10px 10px 10px 10px;
            border: 2px solid #EDEDED;
            margin: 40px auto;
            font-family: PingFang SC, PingFang SC;
            font-weight: 400;
            font-size: 24px;
            line-height: 19px;
            text-align: left;
            font-style: normal;
            text-transform: none;
            text-indent: 10px;
        }
    }

    .defultBox {
        margin-top: 20px;
        width: 100%;
        padding: 0 40px;
        box-sizing: border-box;
        display: flex;
        align-items: center;
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        font-size: 24px;
        color: #B1B1B1;
        line-height: 19px;
        text-align: left;
        font-style: normal;
        text-transform: none;

        .statusimg {
            width: 32px;
            height: 32px;
            margin-right: 20px;
        }
    }

    .btn {
        width: 80%;
        height: 88px;
        background: #E00000;
        border-radius: 100px 100px 100px 100px;
        font-family: PingFang SC, PingFang SC;
        font-weight: 500;
        font-size: 24px;
        color: #FFFFFF;
        line-height: 88px;
        text-align: center;
        font-style: normal;
        text-transform: none;
        margin: 0 auto;
        margin-top: 200px;

    }
}
</style>